﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;
using System.IO;


public partial class Admin_ucControl_ucAddNewItem : System.Web.UI.UserControl
{
    TblCategoryDAO cateDAO = new TblCategoryDAO(Global.CONNECTION_STRING);
    TblItemsDAO itemDAO = new TblItemsDAO(Global.CONNECTION_STRING);
    TblImageDAO imgDAO = new TblImageDAO(Global.CONNECTION_STRING);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {          
            LoadCategory();

            if (rdNo.Checked == true)
            {
                txtmakhuyenmai.Enabled = false;
                txtPricekhuyenmai.Enabled = false;
            }
            else
            {
                txtmakhuyenmai.Enabled = true;
                txtPricekhuyenmai.Enabled = true;
            }

            string id = Request.QueryString["id"];
            if (id == null || id == "")
            {
            }
            else
            {
                LoadDetail();
            }
          
        }
    }
 

    private void LoadCategory()
    {
        drCategory.DataSource = cateDAO.GetCate();
        drCategory.DataValueField = "IDCategory";
        drCategory.DataTextField = "CategoryName";
        drCategory.DataBind();
    }

    // Load Detail
    private void LoadDetail()
    {
        string id = Request.QueryString["id"];
        TblItems item = itemDAO.GetItemByID(Global.ConvertToInt(id));
        txtNumItem.Text = item.ItemNumber;
        txtTenSP.Text = item.ItemName;
       
        txtPrice.Text = item.BaseSellingPrice.ToString();
        txtPricekhuyenmai.Text = item.PriceTaxCode.ToString();
        txtmakhuyenmai.Text = item.TaxPercentageRate.ToString();
        drCategory.SelectedValue = item.IDCategory.ToString();     
        edttsp.Content = item.ItemDescription;        
        LoadImage();

        //if (item.Type.ToString() == "2")
        //{
        //    panelImageView.Visible = true;
        //    panel.Visible = true;
        //}
        //else
        //{
        //    panelImageView.Visible = false;
        //    panel.Visible = false;

        //}

    }
    protected void btnLuu_Click(object sender, EventArgs e)
    {
        bool flag = false;
        try
        {
            TblItems item = new TblItems();
            item.ItemNumber = txtNumItem.Text.Trim();
            item.ItemName = txtTenSP.Text.Trim();
      
            item.IDCategory = Global.ConvertToInt(drCategory.SelectedValue);
        
            item.ItemDescription = Server.HtmlDecode(edttsp.Content);
            item.BaseSellingPrice = float.Parse(txtPrice.Text);
            
            if (rdNo.Checked)
            {
                item.PriceTaxCode = 0;
                item.TaxPercentageRate = 0;
            }
            else
            {
                item.PriceTaxCode = float.Parse(txtPricekhuyenmai.Text.Trim());
                item.TaxPercentageRate = float.Parse(txtmakhuyenmai.Text.Trim());
            }

            string id = Request.QueryString["id"];
            if (id == null || id == "")
            {
                itemDAO.TblItem_InsUp(item, 1);
            }
            else
            {
                item.ItemID = Global.ConvertToInt(id);
                itemDAO.TblItem_InsUp(item, 2);
            }


            flag = true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        if (flag)
        {
            Response.Redirect("~/Admin/ListProduct.aspx");
        }
    }
    protected void btnDelImg_Click(object sender, ImageClickEventArgs e)
    {
        string id = Request.QueryString["id"];
        bool flag = false;
        try
        {
            TblImage imgItem = new TblImage();

            imgItem.IDImage = Convert.ToInt32(((ImageButton)sender).CommandArgument);
            int idXoa = Convert.ToInt32(((ImageButton)sender).CommandArgument);
            imgItem = imgDAO.GetItemImageByID(Global.ConvertToInt(id), idXoa);
            imgDAO.DeleteImgItem(imgItem);

            string filename = imgItem.ImageURL;
            if (File.Exists(Server.MapPath("~\\Admin\\img\\Items\\" + filename)))
            {
                File.Delete(Server.MapPath("~\\Admin\\img\\Items\\" + filename));
            }

            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (flag)
        {
            LoadImage();
        }
    }
    private void LoadImage()
    {
        string id = Request.QueryString["id"];
        grdImgItems.DataSource = imgDAO.GetImage_ByItemID(Global.ConvertToInt(id));
        grdImgItems.DataBind();

    }
    protected void txtmakhuyenmai_TextChanged(object sender, EventArgs e)
    {
        float Price = float.Parse(txtPrice.Text);
        float makhuyenmai = float.Parse(txtmakhuyenmai.Text);
        float PriceTaxPercent = Price - (Price * makhuyenmai / 100);
        txtPricekhuyenmai.Text = PriceTaxPercent.ToString();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtNumItem.Text = txtTenSP.Text = txtPrice.Text = txtmakhuyenmai.Text = string.Empty;
    }
  
    protected void btnUpLoadImage_Click(object sender, EventArgs e)
    {            
        string id = Request.QueryString["id"];
        bool flag = false;
        try
        {
            // Save Image
            TblImage img = new TblImage();
            string path = Server.MapPath("~\\Admin\\img\\Items\\");
            string filename = fileUpload.FileName;
            if (System.IO.File.Exists(path + filename))
            {
                filename = DateTime.Now.ToString("mm") + "_" + DateTime.Now.ToString("ss") + "_" + filename;
            }
            else
            {
                filename = filename.ToString();
            }
            fileUpload.SaveAs(path + filename);
            Session["filename"] = filename;
            img.ImageURL = Session["filename"].ToString();
        
            img.IDItem = Global.ConvertToInt(id);
            imgDAO.TblImage_Ins(img);
            flag = true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        if (flag)
        {  
            Session["filename"] = null;
            LoadImage();
        }
    }
   
}
